package com.ruoyi.web.controller.monitor;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.IGridAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 缓存监控
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/cache")
public class CacheController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private IGridAccountService gridAccountService;

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping()
    public AjaxResult getInfo() throws Exception {
        Properties info = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info());
        Properties commandStats = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info("commandstats"));
        Object dbSize = redisTemplate.execute((RedisCallback<Object>) connection -> connection.dbSize());

        Map<String, Object> result = new HashMap<>(3);
        result.put("info", info);
        result.put("dbSize", dbSize);

        List<Map<String, String>> pieList = new ArrayList<>();
        commandStats.stringPropertyNames().forEach(key -> {
            Map<String, String> data = new HashMap<>(2);
            String property = commandStats.getProperty(key);
            data.put("name", StringUtils.removeStart(key, "cmdstat_"));
            data.put("value", StringUtils.substringBetween(property, "calls=", ",usec"));
            pieList.add(data);
        });
        result.put("commandStats", pieList);
        return AjaxResult.success(result);
    }


    //设备占比
    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping("/cache1")
    public AjaxResult getInfo1() throws Exception {
        Random rand = new Random();
        Map<String, Object> result = new HashMap<>(3);
        List<Map<String, String>> pieList = new ArrayList<>();
        List<Map<String, String>> list = gridAccountService.selectHostCatagory();
        //String[] catagory=new String[]{"主机","路由器","交换机"};
        for (int i = 0; i < list.size(); i++) {
           // Map<String, String> data = new HashMap<>(2);
            Map<String, String> itemrow = list.get(i);

       /*     String name = itemrow.get("templates_name").toString();
            String count = itemrow.get("count");


            data.put("name", name);
            data.put("value", count.toString());*/
            pieList.add(itemrow);
        }
        Properties info = new Properties();
        info.setProperty("total",String.valueOf(gridAccountService.getHostCount()));
        info.setProperty("time",gridAccountService.getLastChangePassTime());
        info.setProperty("num",String.valueOf(gridAccountService.getChangePassCount()));
        result.put("info", info);

        result.put("data", pieList);
        return AjaxResult.success(result);
    }

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping("/cache2")
    public AjaxResult getInfo2() throws Exception {
        Random rand = new Random();
        Map<String, Object> result = new HashMap<>(3);
        List<Map<String, String>> pieList = new ArrayList<>();
        List<Map<String, String>> list = gridAccountService.selectChangePassCatgoryList();
        for (int i = 0; i < list.size(); i++) {
            //Map<String, String> data = new HashMap<>(2);
            Map<String, String> item =(Map<String, String>) list.get(i);
           /* String name = "";
            String count = (String)item.get("count");
            if (item.get("status") != null && item.get("status").toString().equals("0")) {
                name = "成功";
            } else if (item.get("status") != null && item.get("status").toString().equals("1")) {
                name = "失败";
            }
            data.put("name", name);
            data.put("value", String.valueOf(count));*/
            pieList.add(item);
        }
        result.put("data", pieList);
        return AjaxResult.success(result);
    }

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping("/cache3")
    public AjaxResult getInfo3() throws Exception {
        Random rand = new Random();
        Map<String, Object> result = new HashMap<>(3);
        String[] catagory = new String[]{"192.168.0.1", "192.168.0.2", "192.168.0.3", "192.168.0.4", "192.168.0.5"};
        List<String> listName = new ArrayList<>();
        List<Integer> listVal = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            listName.add(catagory[i]);
            listVal.add(rand.nextInt(100));
        }
        result.put("xdata", listName);
        result.put("seriesData", listVal);
        return AjaxResult.success(result);
    }

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping("/cache4")
    public AjaxResult getInfo4() throws Exception {
        Map<String, Object> result = new HashMap<>(3);
        List<Map<String, String>> pieList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, String> data = new HashMap<>(2);
            data.put("name", "策略" + i);
            data.put("value", "10");
            pieList.add(data);
        }
        result.put("data", pieList);
        return AjaxResult.success(result);
    }
}
